<template>
  <div class="app_content">
      <el-form :inline="true" ref="queryForm" :model="queryParams" class="demo-form-inline" :size="size" v-if="showSearch">
        <el-form-item label="岗位编码" prop="postCode">
      <el-input v-model="queryParams.postCode" clearable placeholder="岗位编码" style="width: 240px"/>
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
      <el-input v-model="queryParams.postName" clearable placeholder="岗位名称" style="width: 240px"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
      <el-input v-model="queryParams.status" clearable placeholder="状态" style="width: 240px"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="querySubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-search'"/>
          搜索</el-button>
        <el-button   @click="resetSubmit">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-redo'"/>
          重置</el-button>
      </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            :size="size"
            v-hasRole="['system:post:add']"
            @click="handleAdd"
          >
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-add-one'"/>
          新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            :size="size"
            :disabled="single"
            @click="handleEdit"
            v-hasRole="['system:post:edit']"
          >
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-edit-one'"/>
          修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            :size="size"
            :disabled="multiple"
            @click="handleDelete"
            v-hasRole="['system:post:remove']"
          >
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-delete-five'"/>
          删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <Export  :dc="true"  :dr="true" :exlData="tableData" :tHead="staticData.ExportHead" :tName="'导出的标题'" ></Export>
        </el-col>
  -->
        <!-- <RightToolbar :showSearch="showSearch" @queryTable="getList" :columns="columns"/> -->
        <RightToolbar :showSearch="showSearch" @queryTable='getList' @showSearchClick='showSearchClick' :columns="columns"/>
      </el-row>
      <el-table :border="$border" v-loading="loading" :data="tableData" :height="maxHeight"  style="width: 100%" @selection-change="handleSelectionChange"  :size="size"
      >
      <el-table-column type="selection" width="55" />
      <el-table-column label="编号" prop="id" show-overflow-tooltip key="id" v-if="columns[0].visible">
        <template #default="scope">{{scope.$index+1}}</template>
        </el-table-column>
      <el-table-column label="岗位编码" prop="postCode" show-overflow-tooltip key="postCode"  v-if="columns[1].visible"/>
      <el-table-column label="岗位名称" prop="postName" show-overflow-tooltip key="postName"  v-if="columns[2].visible"/>
      <el-table-column label="岗位顺序" prop="postSort" show-overflow-tooltip key="postSort"  v-if="columns[3].visible"/>
      <el-table-column label="状态" prop="status" :formatter="statusFormat" show-overflow-tooltip key="status"  v-if="columns[4].visible">
        </el-table-column>
      <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip key="createTime"  v-if="columns[5].visible"/>
      <el-table-column label="操作" fixed="right" align="center"  width="250px">
      <template #default="scope">
        <el-button :size="size" type="success"  text @click="handleEdit(scope.row)" v-hasRole="['system:post:edit']"
          >
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-edit-one'"/>
          修改</el-button
          >
          <el-button
          :size="size"
          type="danger"
          text
          @click="handleDelete(scope.row)"
          v-hasRole="['system:post:remove']"
          >
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-delete-five'"/>
          删除</el-button
          >
      </template>
      </el-table-column>
  </el-table>
  <div style="margin-top:10px;position: absolute;right: 0;margin-right:20px">
      <el-pagination
        v-model:currentPage="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
  </div>

  <!-- <el-dialog  v-model="open"  :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" width="600px" append-to-body draggable :fullscreen="fullscreen">
      <template #header="{titleId, titleClass }">
      <div class="my-header">
        <span :id="titleId" :class="titleClass">{{title}}</span>
        <div>
          <component
        style="cursor:pointer;line-height: 24px;margin-right:10px;"
        v-if="fullscreen==false"
              @click="fullscreen=true"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-full-screen-one'"
            />
            <component
            v-else
        style="cursor:pointer;line-height: 24px;margin-right:10px;"
              @click="fullscreen=false"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-off-screen-one'"
            />
        <component
        style="cursor:pointer;line-height: 24px;"
              @click="cancel"
              class="menu-icon"
              theme="outline"
              size="14"
              strokeWidth="3"
              :is="'icon-close'"
            />
        </div>
        
      </div>
    </template>
    <el-form ref="formDataRef" :model="formData" :rules="rules" label-width="80px" :size="size">
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="formData.postName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="formData.postCode" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number v-model="formData.postSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="formData.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
    <template #footer>
        <div  class="dialog-footer">
        <el-button type="primary" @click="submitForm" :size="size">确 定</el-button>
        <el-button @click="cancel" :size="size">取 消</el-button>
      </div>
    </template>
    </el-dialog> -->
    <el-drawer
      :title="title"
      v-model="open"
      direction="rtl"
      destroy-on-close
      size="50%"
      :close-on-click-modal="false"
    >
    <el-form ref="formDataRef" :model="formData" :rules="rules" label-width="80px" :size="size">
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="formData.postName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="formData.postCode" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number v-model="formData.postSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="formData.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
    <template #footer>
        <div  class="dialog-footer">
          <el-button type="primary" @click="submitForm" :size="size">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-correct'"/>
          确 定</el-button>
        <el-button @click="cancel" :size="size">
          <component :theme="iconTheme" :size="iconSize" :strokeWidth="iconStrokeWidth" :is="'icon-return'"/>
          取 消</el-button>
      </div>
    </template>
    </el-drawer>
  
  </div>
  
</template>

<script  >
import RightToolbar from '@/components/RightToolbar/index.vue';
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/system/post";
import { computed, ref ,reactive,onMounted,getCurrentInstance,toRefs} from 'vue'
import { useStore } from 'vuex';
import { useI18n } from 'vue-i18n';
import {
Delete,
Edit,
Plus,
} from '@element-plus/icons-vue'

export default defineComponent({
setup(){
  const { proxy } = getCurrentInstance();//关键代码
  const queryForm=ref(null)
  const store = useStore();
  const formDataRef=ref(null)
  const { t } = useI18n();
  const staticData=reactive({
    formData:{
    },
     // 表单校验
     rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      },
    maxHeight:store.getters['setting/mode']=='horizontal'?document.body.clientHeight - 265:store.getters['setting/isFullScreen']!=false ?document.body.clientHeight - 360:document.body.clientHeight - 320,
    ids:[],
    postName:[],
    single:true,
    multiple:true,
    showSearch:true,
    background:false,
    disabled :false,
    search:'',
    tableData:[],
    // 日期范围
    dateRange: [],
    // 菜单状态数据字典
    statusOptions: [],
    //是否全屏
    fullscreen:false,
    // 是否显示弹出层
    open: false,
    // 弹出层标题
    title: "",
    // 查询参数
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      postCode: undefined,
      postName: undefined,
      status: undefined
    },
    total:0,
    loading:true, 
    ExportHead:{
      id: "编号",
      name: "角色名称",
      age: "权限字符",
      xb: "显示顺序",
      des: "状态",
      date: "创建时间",
    },
    // 列信息
    columns: [
      { key: 0, label: `编号`, visible: true },
      { key: 1, label: `岗位编码`, visible: true },
      { key: 2, label: `岗位名称`, visible: true },
      { key: 3, label: `岗位顺序`, visible: true },
      { key: 4, label: `状态`, visible: true },
      { key: 5, label: `创建时间`, visible: true },
      
    ],
  })
  const size = computed(() => {
        return store.getters['setting/size'];
  });

  const small =computed(() => {
    return size.value=='small'?true:false
  });

  const filterTableData = computed(() =>
    staticData.tableData.filter(
      (data) =>
        !staticData.search.value ||
        data.name.toLowerCase().includes(staticData.search.value.toLowerCase())
    )
  )
    onMounted(() => {
      proxy.$getDicts("sys_normal_disable").then(response => {
        staticData.statusOptions = response.data;
      });
      proxy.getList()
      })

  /**
   * 重置表单效验方法
   */
   const resetForm = (formEl) => {
    if (!formEl.value) return
    formEl.value.resetFields()
  }


  const methods = {
     getList(){
        staticData.loading = true;
        listPost(staticData.queryParams).then(
          response => {
            staticData.tableData = response.rows;
            staticData.total = response.total;
            staticData.loading = false;
          }
      );
     },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      const data=proxy.$selectDictLabel(staticData.statusOptions, row.status)
      let stateClass
      // if(data=='正常'){
      //   stateClass='status_success'
      // }else{
      //   stateClass='status_error'
      // }
      
      return data;
    },
    // 取消按钮
    cancel() {
      staticData.open = false;
      proxy.reset();
    },
    // 表单重置
    reset() {
      staticData.formData = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      resetForm(formDataRef)
    },
     /**
      * 显示或隐藏搜索条件
      **/
     showSearchClick(data){
       staticData.showSearch=data
     },
    /**表格多选 */
    handleSelectionChange(selection){
      staticData.ids = selection.map(item => item.postId);
      staticData.postName = selection.map(item => item.postName);
      staticData.single = selection.length != 1;
      staticData.multiple = !selection.length;

    },
     /** 新增按钮操作 */
    handleAdd() {
      proxy.reset();
      staticData.fullscreen=false
      staticData.open = true;
      staticData.title = "添加岗位";
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      proxy.reset();
      staticData.fullscreen=false
      const postId = row.postId || staticData.ids
      getPost(postId).then(response => {
        staticData.formData = response.data;
        staticData.formData.postSort=parseInt(response.data.postSort)
        staticData.open = true;
        staticData.title = "修改岗位";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      formDataRef.value.validate(valid => {
        if (valid) {
          if (staticData.formData.postId != undefined) {
            updatePost(staticData.formData).then(response => {
              proxy.Success("修改成功");
              staticData.open = false;
              proxy.getList();
            });
          } else {
            addPost(staticData.formData).then(response => {
              proxy.Success("新增成功");
              staticData.open = false;
              proxy.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || staticData.ids;
      const postName = row.postName || staticData.postName;
      
      ElMessageBox.confirm('是否确认删除岗位名称为"' + postName + '"的数据项？', t('confirm.title'), {
          confirmButtonText: t('btn.confirm'),
          cancelButtonText: t('btn.cancel'),
          type: "warning"
        })
      .then(function() {
        return delPost(postIds);
      }).then(() => {
        proxy.getList();
        proxy.Success("删除成功");
      }).catch(() => {});
    },
    resetSubmit(){
      staticData.dateRange=[]
      queryForm.value.resetFields();
      proxy.getList()
    },
    querySubmit(){
      staticData.queryParams.pageNum = 1;
      proxy.getList()
    },
    handleSizeChange (val) {
      console.log(`${val} items per page`)
      staticData.queryParams.pageSize =val;
      proxy.getList()
    },
    handleCurrentChange(val){
      console.log(`current page: ${val}`)
      staticData.queryParams.pageNum =val;
      proxy.getList()
    }
   }
    return {
      Delete,
      Edit,
      Plus,
      size,
      small,
      queryForm,
      formDataRef,
      ...methods,
      filterTableData,
      ...toRefs(staticData)
    }
}


})



</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
margin-top: 10px;
}
.demo-pagination-block .demonstration {
margin-bottom: 16px;
}
</style>
<style scoped>
.app_content{
  
  margin-top: 30px;
  margin-left: 10px;
}
.mb8{
  margin-bottom: 10px;
}
</style>